<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>货品查询</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link href="../../layuiadmin/layui/css/layui.css" media="all" rel="stylesheet">
    <link href="../../layuiadmin/style/admin.css" media="all" rel="stylesheet">
</head>
<body>
<div class="layui-card-body">
    <form>
        <label>客户编码：</label>
        <div class="layui-input-inline">
            <input class="layui-input" id="vendorCode" type="text">
            <input autocomplete="off" class="layui-input" id="vendorId" type="hidden">
        </div>
        <label style="padding-left:45px;"></label>
        <button class="layui-btn layui-btn-radius" data-type="reload" id="search" type="button">查&nbsp;&nbsp;询</button>
        <!--            <a class="layui-btn layui-btn-danger layui-btn-sm" style="margin-left: 20px;" id="T" href="javascript:getSUP();">+录入货品</a>-->
        <br>
        <table class="layui-hide" id="vendortable" lay-filter="vendortable"></table>
        <script id="toolbarDemo" type="text/html">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
            </div>
        </script>
    </form>
</div>
<div class="layui-card-body">
    <form>
        <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:getSUP();" id="T"
           style="margin-left: 20px;">+为客商添加货品</a>
        <table class="layui-hide" id="itemtable" lay-filter="demo"></table>
    </form>
</div>
<script src="../../layuiadmin/layui/layui.js?t=1"></script>
<!-- 操作按钮 -->
<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use('table', function () {
        var table = layui.table;
        var $ = layui.$;
        var layuiTable = layui.table;
        var vipId;
        //加载初始化Vendor模块
        var tabVip = table.render({
            elem: '#vendortable'
            , url: 'http://localhost:8086/selectVendorForPage'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , data: []
            , cols:
                [
                    [
                        {field: 'vendorId', width: 80, title: 'ID', sort: true}
                        , {field: 'vendorCode', width: 90, title: '客户编码'}
                        , {field: 'vendorName', width: 100, title: '客户名称'}
                        , {field: 'registeredArea', width: 110, title: '注册地区'}
                        , {field: 'phone', width: 110, title: '电话'}
                        , {field: 'enabledFlag', width: 90, title: '是否启用'} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                        , {field: 'categoryCode', title: '招卖招卖', sort: true}
                        , {field: 'description', title: '备注', width: '30%', minWidth: 100}
                        // ,{fixed: 'right', width:178, align:'left', toolbar: '#barDemo'} 	//添加工具条
                    ]
                ]
            , page: true
            , done: function (data) {
            }
        });
        //为工具条添加监听
        //这里括号里对应table设置的lay-filter属性！！
        table.on('tool(demo)', function (obj) {
            //获取当前行数据
            var data = obj.data;
            //获取event对应的值
            var event = obj.event;
            //获取当前行的dom对象
            var tr = obj.tr;

            if (event === 'detail') {		//如果点击了查看按钮
                //执行查看详情的方法

            } else if (event === 'del') {	//如果点击了删除按钮
                //执行异步删除方法
                //删除方法
                var layer = layui.layer;
                layer.confirm('真的删除行么', function (index) {
                    //index表示第几次弹出层不是索引值，也不是主键等信息
                    //刷新页面重置
                    $.ajax({
                        url: 'http://localhost:8086/delItemForVendor',
                        data: {'vendorId': vipId, 'itemId': data.itemId},
                        type: "Post",
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            layer.msg(data.msg);
                            /*location.reload(); //删除成功后再刷新*/
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);
                        },
                        error: function (data) {
                            $.messager.alert('错误', data.msg);
                        }
                    });
                })
            } else if (event === 'edit') {	//如果点击了修改按钮
                //执行修改方法
            }
        });
        var active = {
            reload: function () {
                //搜索，执行重载
                table.reload('vendortable', {
                    url: 'http://localhost:8086/selectVendorForPage'
                    , where: {
                        /*id: document.getElementById("uid").value*/
                        vendorCode: document.getElementById("vendorCode").value
                    }
                });
            }
        };
        //客户信息查询
        $('#search').on('click', function () {
            var type = $(this).data('type');
            console.info(type);
            active[type] ? active[type].call(this) : '';
        });
        //加载初始化item模块
        var tabAdjustmontRecord = layuiTable.render({
            elem: '#itemtable'
            // ,url:'http://localhost:8086/selectVendorForPage'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , data: []
            , cols:
                [
                    [
                        {field: 'itemId', width: 80, title: 'ID', sort: true}
                        , {field: 'itemCode', width: 90, title: '存货编码'}
                        , {field: 'itemName', width: 100, title: '存货名称'}
                        , {field: 'itemSpecification', width: 110, title: '规格'}
                        , {field: 'enabledFlag', width: 110, title: '是否启用'}
                        , {field: 'description', title: '备注', width: '30%', minWidth: 100} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                        /*,{field:'bidderName', title: '投标人（单人）', sort: true}
                        ,{field:'bidderPhone', title: '投标人电话', sort: true}
                        ,{field:'registeredArea', title: '注册地区'}
                        ,{field:'remarks', width:137, title: '备注', sort: true}*/
                        , {fixed: 'right', width: 178, align: 'left', toolbar: '#barDemo'} 	//添加工具条
                    ]
                ]
            , page: true
            , done: function (data) {
            }
        });


        //获取VIP表行事件
        table.on('row(vendortable)', function (obj) {
            var data = obj.data;//获取点击行数据
            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');

            ////***单击选中单选框
            /*obj.tr.find('div.layui-unselect.layui-form-radio')[0].click();*/

            vipId = parseInt(data.vendorId);//vip类型id
            document.getElementById("vendorId").value = vipId;

            //控制台输出看看是否能获取到VIPid
            console.log(vipId);


            //调整记录附件，根据VIPid查询对应的调整记录信息
            tabAdjustmontRecord.reload
            ({
                //查询路径
                url: 'http://localhost:8086/selectItemForVendor',
                where:
                    { //设定异步数据接口的额外参数，比如设置多条件查询的参数
                        vendorId: vipId
                    },
                //从第一页开始
                page:
                    {
                        curr: 1
                    }
            });
        });
    });

    function getSUP() {
        var layer = layui.layer;
        if (document.getElementById("vendorId").value == null || document.getElementById("vendorId").value == '') {
            alert("未选中客户！")
        } else {
            layer.open({
                type: 2,
                title: "+为客商添加货品",
                area: ['800px', '480px'],
                skin: 'layui-layer-rim',
                content: ['http://localhost:8086/page?url=new/insertItemForVendor.html'],
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);
                    // 取到弹出层里的元素，并把编辑的内容放进去
                    body.find("#vendorId").val(document.getElementById("vendorId").value);
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            });
        }
    }

</script>
</body>
</html>